package no.saiboten.milkywave.graph;

import java.util.LinkedList;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

@Component
public class GraphService {
	private LinkedList<Double> hourlyRegistrations;

	int maxRegistration;

	public GraphService() {
		hourlyRegistrations = new LinkedList<Double>();
		maxRegistration = 5;
	}

	public void registerNewPlot(double value) {
		hourlyRegistrations.addLast(value);

		if (hourlyRegistrations.size() == maxRegistration) {
			hourlyRegistrations.removeFirst();
		}
	}

	public LinkedList<Double> getValues() {
		return hourlyRegistrations;
	}

	public String getGraphImg() {
		String listOfValues = "";

		for (Double value : hourlyRegistrations) {
			listOfValues += value + ",";
		}

		listOfValues = StringUtils.chop(listOfValues);

		return "http://chart.apis.google.com/chart?chxt=y&chbh=a&chs=240x145&cht=bvg&chco=A2C180&chds=0,300&chd=t:"
				+ listOfValues + "&chtt=Vannf%C3%B8ring+per+time";
	}

}
